package com.team.student.controller;

import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.team.student.entity.Grade;
import com.team.student.entity.Stu;
import com.team.student.entity.StuCondition;
import com.team.student.entity.StuExample;
import com.team.student.service.FindStuAndGrade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class StuController {

    @Autowired
    private FindStuAndGrade findStuAndGrade;
    @RequestMapping(value = "/showStu")
    @PostMapping()
    public String showStu(Model model){
        List<Stu> list = findStuAndGrade.FindStuAndGrade();
        model.addAttribute("stus",list);
        return "index";
    }

    @RequestMapping(value = "/delStuAndGrade")
       public String delStuAndGrade(Integer gradeid){
        try {
            findStuAndGrade.delStuAndGrade(gradeid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:findAllGrade";

        }

        @RequestMapping(value = "delStu")
        public String delStu(Integer studentno,Model model){
            Integer temp = -1;
                   temp = findStuAndGrade.delStu(studentno);
             model.addAttribute("temp",temp);
                   if (temp > 0){
                       return "redirect:findStuLim";
                   }else {
                       return "redirect:findStuLim";
                   }
        }

    @RequestMapping(value = "getGradeName")
        public String getGradeName(Model model){
        List<Grade> list = findStuAndGrade.getAllGrade();
        model.addAttribute("grade",list);
        return "addStu";
    }

    @RequestMapping(value = "/addStu")
    public String addStu(Stu stu){
        System.out.println(stu.getAddress());

        findStuAndGrade.addStu(stu);
        return "redirect:showStu";
    }

    @RequestMapping(value = "/getSingleStu")
    public String getSingleStu(Integer studentno,Model model){
        List<Grade> list = findStuAndGrade.FindAllGrade();
        Stu singleStu = findStuAndGrade.findSingleStu(studentno);
        model.addAttribute("grade",list);
        model.addAttribute("Stu",singleStu);
        return "updateStu";
    }

    @RequestMapping(value = "/updateStu")
    public String updateStu(Stu stu){
        findStuAndGrade.updateStu(stu);
        return "redirect:showStu";
    }

    /*
    * 查询全部的分页查询
    * */
    @RequestMapping(value = "/findStuLim")
    public String findStuLim(Integer pageNo, Model model){
        PageInfo<Stu> stuLim = findStuAndGrade.getAllStuLim(pageNo==null?1:pageNo,3);
        model.addAttribute("stus",stuLim);
        return "index";
    }

    /*
    * 带条件的分页查询
    * */
    @RequestMapping(value = "findStuCondition")
    public String findStuCondition(StuCondition stuCondition, Model model){

        PageInfo<Stu> stuPageInfo = findStuAndGrade.findStuCondition(stuCondition);
        model.addAttribute("stus",stuPageInfo);
        model.addAttribute("stuCondition",stuCondition);
        return "index";

    }

    /*
    * 带条件的连表分页查询
    * */
    @RequestMapping(value = "findStuGradeCondition")
    public String findStuGradeCondition(StuCondition stuCondition,Model model){
        PageInfo<Stu> stuPageInfo = findStuAndGrade.selectStuAndGrade(stuCondition);
        model.addAttribute("stus",stuPageInfo);
        model.addAttribute("stuCondition",stuCondition);
        return "index";
    }
}
